#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "imysql_frontapp.h"
#include "conndlg.h"


IMPLEMENT_APP( IMySQL_FrontApp )

IMPLEMENT_CLASS( IMySQL_FrontApp, wxApp )

BEGIN_EVENT_TABLE( IMySQL_FrontApp, wxApp )

END_EVENT_TABLE()


IMySQL_FrontApp::IMySQL_FrontApp()
{
    Init();
}


void IMySQL_FrontApp::Init()
{

}

bool IMySQL_FrontApp::OnInit()
{    
#if wxUSE_XPM
	wxImage::AddHandler(new wxXPMHandler);
#endif
#if wxUSE_LIBPNG
	wxImage::AddHandler(new wxPNGHandler);
#endif
#if wxUSE_LIBJPEG
	wxImage::AddHandler(new wxJPEGHandler);
#endif
#if wxUSE_GIF
	wxImage::AddHandler(new wxGIFHandler);
#endif
	ServerInfo* serverInfo = new ServerInfo();
	serverInfo->mysql = _InitMySQL(NULL);
	ConnDlg connDlg(serverInfo,wxID_ANY,wxT("iMySQL-Front"),wxDefaultPosition);
	int res = connDlg.ShowModal();
    if ( res == wxID_CANCEL){
		//_CloseMySQL(serverInfo->mysql);
		return OnExit();
	}
	iMySQL_Front* mainWindow = new iMySQL_Front( /*&mutex,*/NULL, serverInfo );
#ifdef __WXMSW__
	mainWindow->ShowFullScreen(true,wxMAXIMIZE);//Show(true);
#else
	mainWindow->Show(true);
	mainWindow->Maximize(true);
#endif
    return true;
}


int IMySQL_FrontApp::OnExit()
{    
	return wxApp::OnExit();
}


void IMySQL_FrontApp::OnUnhandledException()
{
    // this shows how we may let some exception propagate uncaught
    try
    {
        throw;
    }
    /*catch ( UnhandledException& )
    {
        throw;
    }*/
    catch ( ... )
    {
		throw;
        wxMessageBox(_T("Unhandled exception caught, program will terminate."),
                     _T("wxExcept Sample"), wxOK | wxICON_ERROR);
    }
}

void IMySQL_FrontApp::OnFatalException()
{
    wxMessageBox(_T("Program has crashed and will terminate."),
                 _T("wxExcept Sample"), wxOK | wxICON_ERROR);
}

bool IMySQL_FrontApp::OnExceptionInMainLoop()
{
    try
    {
        throw;
    }
    /*catch ( int i )
    {
        wxLogWarning(_T("Caught an int %d in MyApp."), i);
    }
    catch ( MyException& e )
    {
        wxLogWarning(_T("Caught MyException(%s) in MyApp."), e.what());
    }*/
    catch ( ... )
    {
        wxLogWarning(_T("Caught an exception."));
        throw;
    }

    return true;
}